const aliSdk = require("../../utils/aliIot-sdk")
const app = getApp()

Component({
    properties: {},
    data: {
        token: '',
        value: {
            size: 100,
            stroke_width: 10,
            temp: 0,
            humi: 0,
            distance: 0,
        },
        color: {
            '0%': '#10557D',
            '100%': '#62A8C5',
        }
    },
    lifetimes: {
        attached: function () {
            this.setToken();
            if (this.data.token != '') {
                this.getTemp();
                this.data.timer = setInterval(() => {
                    this.getTemp();
                }, 1000);
            }else {
                wx.showToast({
                    title: '请先登录',
                    icon: 'none',
                    duration: 2000
                })
            }

        }

    },
    methods: {
        getTemp() {
            wx.request({
                url: 'http://43.143.141.18:8080/api/v1/'+this.data.token+'/attributes?clientKeys=distance,temperature,humidity',
                method: 'GET',
                success: (res) => {
                    // console.log(res)
                    this.setData({
                        'value.temp': res.data.client.temperature.toFixed(1),
                        'value.humi': res.data.client.humidity.toFixed(1),
                        'value.distance': res.data.client.distance.toFixed(0),
                    })
                },
                fail: (res) => {
                    console.log(res)
                }
            })
        },
        setToken() {
            this.setData({
                token: app.globalData.token
            })
            console.log("this.data.toke")
            console.log(this.data.token)
        }
    }
})